/*
Eteria IRC Client, an RFC 1459 compliant client program written in Java.
Copyright (C) 2000  Javier Kohen <jkohen at tough.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

import java.text.MessageFormat;
import java.util.*;

import ar.com.jkohen.util.Resources;
import ar.com.jkohen.util.ConfigurationProperties;

public class IRCServices implements Observer
{
    private static EIRC eirc;
	private Resources res;
    
    private static int NICKS = 0;
    private static int CHANS = 1;
    private static int MEMOS = 2;
	private static int BOTS = 3;
	private static int HELP = 4;
	private static int OPER = 5;
	private static int HOSTS = 6;
	
	private static int SERV_NUM = 7;
	private static String serv[];
	
	private static String eggs;

	private static final MessageFormat reg_str = new MessageFormat(Resources.getString("services.ns.reg"));
	private static final MessageFormat ident_str = new MessageFormat(Resources.getString("services.ns.ident"));
	private static final MessageFormat ghost_str = new MessageFormat(Resources.getString("services.ns.ghost"));
	
    public IRCServices(EIRC eirc)
    {
    	this.eirc = eirc;
		this.serv = new String[SERV_NUM];
		this.eggs = "";
    }

	public boolean isService(String name)
	{
		for (int i = 0; i < SERV_NUM; i++)
			if (name.equalsIgnoreCase(serv[i]))
				return(true);
				
		if (eggs != null && !eggs.equals(""))
		{
			StringTokenizer st = new StringTokenizer(eggs, ",");
			while (st.hasMoreTokens())
			{
				if (name.equalsIgnoreCase(st.nextToken()))
					return(true);
			}
		}
				
		return(false);
	}

    public void identifyNick(String passwd)
    {
    	if (passwd == null || passwd.equals("") || serv[NICKS] == null || serv[NICKS].equals(""))
    		return;
    		
    	Object [] o = { passwd };
		String p[] = { serv[NICKS], ident_str.format(o) };
		eirc.sendMessage("PRIVMSG", p);
    }
	
    public void op()
    {
		if (serv[CHANS] == null || serv[CHANS].equals(""))
    		return;
			
		String p[] = { serv[CHANS], res.getString("services.cs.op") };
		eirc.sendMessage("PRIVMSG", p);
    }
    
    public void killGhost(String nick, String passwd)
    {
    	if (passwd == null || passwd.equals("") || serv[NICKS] == null || serv[NICKS].equals(""))
    		return;
    		
    	Object [] o = { nick, passwd };
		String p[] = { serv[NICKS], ghost_str.format(o) };
		eirc.sendMessage("PRIVMSG", p);
    }
	
    public void registerNick(String passwd, String email)
    {
    	if (passwd == null || passwd.equals("") || serv[NICKS] == null || serv[NICKS].equals(""))
    		return;
    		
    	Object [] o = { passwd, email };
		String p[] = { serv[NICKS], reg_str.format(o) };
		eirc.sendMessage("PRIVMSG", p);
	}
	
	public boolean isIdPrompt(String service, String message)
	{
		return(message.indexOf(res.getString("services.ns.prompt")) >= 0 && service.equals(serv[NICKS]));
	}
	
	public boolean isPassBad(String service, String message)
	{
		return(message.indexOf(res.getString("services.ns.loginbad")) >= 0 && service.equals(serv[NICKS]));
	}
	
	public boolean isPassOk(String service, String message)
	{
		return(message.indexOf(res.getString("services.ns.loginok")) >= 0 && service.equals(serv[NICKS]));
	}
	
    public void update(Observable o, Object arg)
    {
		ConfigurationProperties props = (ConfigurationProperties) o;

 		if (arg == null || arg.equals("service_bots.nicks"))
		    this.serv[NICKS] = props.getString("service_bots.nicks");
			
		if (arg == null || arg.equals("service_bots.chans"))
		    this.serv[CHANS] = props.getString("service_bots.chans");
			
		if (arg == null || arg.equals("service_bots.memos"))
		    this.serv[MEMOS] = props.getString("service_bots.memos");
			
		if (arg == null || arg.equals("service_bots.bots"))
		    this.serv[BOTS] = props.getString("service_bots.bots");
			
		if (arg == null || arg.equals("service_bots.help"))
		    this.serv[HELP] = props.getString("service_bots.help");
			
		if (arg == null || arg.equals("service_bots.oper"))
		    this.serv[OPER] = props.getString("service_bots.oper");

		if (arg == null || arg.equals("service_bots.hosts"))
		    this.serv[HOSTS] = props.getString("service_bots.hosts");
			
		if (arg == null || arg.equals("others_bots"));
		    this.eggs = props.getString("others_bots");
    }
}